<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of staticStars
 *
 * @author miroslav
 */
//include_once dirname(__FILE__) . '/db_connect.php';

class staticStars {

    private $StarNum = 0;
    private $Stars;
    private $hostelID;

    function __construct($numberOfStars) {
        $this->StarNum = $numberOfStars;
    }

    public function printStars($avg) {
        $html = '<div class="s_rate_choice">';
        $html_inner = '<div id="s_r0" class="s_rate_widget">' . "\n";
        for ($i = 0; $i < $this->StarNum; $i++) {
            $a = $i + 1;
            $html_inner .= '<div class="s_star_' . $a . ' s_ratings_stars s_ratings_over"></div>' . "\n";
        }
        for ($i = $this->StarNum; $i < 5; $i++) {
            $a = $i + 1;
            $html_inner .= '<div class="s_star_' . $a . ' s_ratings_stars"></div>' . "\n";
        }
        $html_inner.= '<div class="total_votes">' . round($avg,1) . '</div>';
        $html_inner .= '</div>';
        $html = $html_inner . '</div>' . "\n";
        //echo $html;
        return $html_inner;
    }

    public function getFromDb($hostelID) {
        $query = "SELECT RateNum, RateSum FROM rateing WHERE IDRate=$hostelID; "; //rilise right query 

        $db = new db_connect();
        $conn = $db->connect();

        $result = mysql_query($query, $conn);
        $row = mysql_fetch_array($result);
        $this->StarNum = round($row["RateSum"] / $row["RateNum"]);
        //$this->printStars();

        return round($row["RateSum"] / $row["RateNum"], 1);
    }

    public function getFromArray($hostelIDarray) {
        $this->Stars = $hostelIDarray;
        $html = '<div class="rate_choice">
            <table>';

        for ($i = 0; $i < sizeof($hostelIDarray); $i++) {
            $this->hostelID = $hostelIDarray[$i][0]; //upisuje idHostela koje kasnije cita printStars() za upis ratingWigdetID-a
            $this->StarNum = $hostelIDarray[$i][1];  //upisuje broj zvezdica koje kasnije cita printStars()
            $html = '<tr><td>' . $hostelIDarray[$i][2] . '</td><td>';
            $html .= $this->printStars();
            $html .= '</td></tr>';
        }

        $html .='</table></div>';
        echo $html;
        return $html;
    }

}

?>
